<?php
include('../conn.php');
/*
从购物车获取数据添加订单
*/
error_reporting(0);
// 需传参数
$userid = $_REQUEST['userid'];
$addressid = $_REQUEST['addressid'];
$shopid = $_REQUEST['shopid'];
$buyFreightRisk = $_REQUEST['buyFreightRisk'];
$freightRisk = $_REQUEST['freightRisk'];
$distribution_fee = $_REQUEST['distribution_fee'];
$carid = $_REQUEST['carid'];
$msg = $_REQUEST['msg'];
$fanquanid = $_REQUEST['fanquanid'];
// 需传参数 end

// 解析 
$msgs = explode(',', $msg);
if ($msgs[0] == 'false') {
	$msgs[0] = '';
}elseif ($msgs[1] == 'false') {
	$msgs[1] = '';
}
$fanquanids = explode(',', $fanquanid);
if ($fanquanids[0] == 'false') {
	$fanquanids[0] = '';
}elseif ($fanquanids[1] == 'false') {
	$fanquanids[1] = '';
}
// 解析 end

// 生成订单号函数
function build_order_no(){
    return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}
// 生成订单号函数 end

// 状态 
$state = 0;
$state1 = 1;
$state7 = 7;
$is_use = 0;
$is_use1 = 1;
// 状态 end

if ($userid != '' && $shopid != '' && $carid != '' && $distribution_fee != '') {
	// 查找送货地址
	if ($addressid != '') {
		$query = mysql_query("select * from address where id=".$addressid);
	}else{
		// 默认地址
		$query = mysql_query("select * from address where userid=".$userid." and state=".$state1);
		// 默认地址 end
	}
	$myaddress = mysql_fetch_assoc($query);
	$address=$myaddress[province].",".$myaddress[quyu].",".$myaddress[city].",".$myaddress[address].",".$myaddress[name].",".$myaddress[tel];
	// 查找送货地址 end

	// 生成订单号
	$numbers = build_order_no();
	// 生成订单号 end

	// 区分海淘和快购
	$query = mysql_query("select * from car where userid=".$userid." and carid in (".$carid.") and state=".$state);
	$htgoodsNum = 0; // 海淘商品个数
	while ($row = mysql_fetch_assoc($query)) {
		// 查找商品类型（快购或海淘）
		$sql = mysql_query("select type from zifenlei where id=(select zixilie from goods where goodsid=".$row[goodsid].")");
		$zixilie = mysql_fetch_assoc($sql);
		if ($zixilie[type] == 0) {
			$htcarid.=','.$row[carid]; 
			$htgoodsNum+= $row[number];
		}else{
			$kgcarid.=','.$row[carid]; 
		}
		// 查找商品类型（快购或海淘） end
	}

	$htcarid = ltrim($htcarid,',');
	$kgcarid = ltrim($kgcarid,',');
	// 区分海淘和快购 end

	// 查找商店信息 
	$query = mysql_query("select * from shop where shopid=".$shopid);
	$shop = mysql_fetch_assoc($query);
	// 查找商店信息 end

	$query=mysql_query("select * from car where shopid='".$rowf[shopid]."' and userid='".$_REQUEST[userid]."' and carid in (".$carid.") and state='".$state."'");
	// 记录
	$ordersids = '';
	$total = 0; // 所有商品总价
	$total += $freightRisk;
	// 记录
	// 生成海淘订单
	if ($htcarid != '') {
		$query = mysql_query("select * from car where carid in (".$htcarid.") and state=".$state);
		$totalprice = 0;
		while($row = mysql_fetch_array($query)){
			$totalprice += $row[price]*$row[number];
		}

		// 计算运费险
		// if ($buyFreightRisk == 1) {
		// 	$query = mysql_query("select * from freightrisk");
		// 	$freightRisk = 0;
		// 	while ($row = mysql_fetch_assoc($query)) {
		// 		if ($htgoodsNum >= $row[goodsNum]) {
		// 			$freightRisk = $row[freightRisk];
		// 		}
		// 	}
		// 	$totalprice+= $freightRisk;
		// }
		$total+= $totalprice;
		// 计算运费险 end

		// 使用返券 
		if ($fanquanids[0] != '') {
			// 计算价格 
			$query = mysql_query("select price from myfanquan where fanquanid=".$fanquanids[0]."");
			$fanquan_price = mysql_fetch_assoc($query);
			$total -= $fanquan_price['price'];
			// 计算价格 end

			// 更新返券为已经使用
			$query = mysql_query("UPDATE myfanquan set is_use = ".$is_use1." where fanquanid=".$fanquanids[0]);
			// 更新返券为已经使用 end
		}
		// 使用返券 end
		$totalprice+= $distribution_fee;
		// 添加到订单表
		$query = mysql_query("INSERT INTO goodsorders (numbers,totalprice,userid,address,shopid,buyFreightRisk,msg,type,distribution_fee) VALUES ('$numbers','$total','$userid','$address','$shopid','$buyFreightRisk','$msgs[0]','$state','$distribution_fee')");
		$ordersid = mysql_insert_id();
		$ordersids.= ','.$ordersid;
		// 添加到订单表 end

		// 更新购物车信息
		$query = mysql_query("update car set ordersid='".$ordersid."',state=".$state7." where carid in (".$htcarid.") and state=".$state);
		// 更新购物车信息 end
		 
		//更新商品库存和销量
		$htcar = explode(",", $htcarid);
		$htcarArray = count($htcar); 
		for($i=0;$i<$htcarArray;$i++){
			    $query = mysql_query("select * from car where carid=".$htcarArray[$i]);
				$row = mysql_fetch_assoc($query);

				// 更新商品的销量和库存
				$query = mysql_query("update goods set number=number-".$row[number].",xiaoliang=xiaoliang+".$row[number]." where goodsid=".$row[goodsid]);
				// 更新商品的销量和库存 end

				// 减少该商品规格的数量
				if ($row[sTitle] != '') {
					$query = mysql_query("update goodsselect set number=number-".$row[number]." where goodsid=".$row[goodsid]." and fTitle='".$row[fTitle]."' and sTitle='".$row[sTitle]."'");
				}else{
					$query = mysql_query("update goodsselect set number=number-".$row[number]." where goodsid=".$row[goodsid]." and fTitle='".$row[fTitle]."'");
				}
				// 减少该商品规格的数量 end
		}

		// 信息推送
		$url = "http://120.24.0.10/xzpf/api/jpush/jpush.php?state=".$state."&ordersid=".$ordersid;
		file_get_contents($url);
		// 信息推送 end
	}
	// 生成海淘订单 end

	// 生成快购订单
	if ($kgcarid != '') {
		$query = mysql_query("select * from car where carid in (".$kgcarid.") and state=".$state);
		$totalprice = 0;
		while($row = mysql_fetch_array($query)){
			$distribution_fee += $shop[distribution_fee];
			$totalprice += $row[price]*$row[number]+$shop[distribution_fee];
		}
		$total+= $totalprice;

		// 使用返券 
		if ($fanquanids[1] != '') {
			// 计算价格 
			$query = mysql_query("select price from myfanquan where fanquanid=".$fanquanids[1]."");
			$fanquan_price = mysql_fetch_assoc($query);
			$total -= $fanquan_price['price'];
			// 计算价格 end

			// 更新返券为已经使用
			$query = mysql_query("UPDATE myfanquan set is_use = ".$is_use1." where fanquanid=".$fanquanids[1]);
			// 更新返券为已经使用 end
		}
		// 使用返券 end
		// 添加到订单表
		$query = mysql_query("INSERT INTO goodsorders (numbers,totalprice,userid,address,shopid,msg,type,distribution_fee) VALUES ('$numbers','$total','$userid','$address','$shopid','$msgs[1]','$state1','$distribution_fee')");
		$ordersid=mysql_insert_id();
		$ordersids.= ','.$ordersid;
		// 添加到订单表 end

		// 更新购物车信息
		$query = mysql_query("update car set ordersid='".$ordersid."',state=".$state7." where carid in (".$kgcarid.") and state=".$state);
		// 更新购物车信息 end

		// 信息推送
		$url = "http://120.24.0.10/xzpf/api/jpush/jpush.php?state=".$state."&ordersid=".$ordersid;
		file_get_contents($url);
		// 信息推送 end
	}
	// 生成快购订单 end

	$ordersid = ltrim($ordersids,',');
	// $list = array('numbers' => $numbers, 'ordersid' => $ordersid, 'total' => $zixilie[type] == 0?$total+$distribution_fee:$total);
	$list = array('numbers' => $numbers, 'ordersid' => $ordersid, 'total' => $total);

	$arr = array('code' => 200, 'msg' => '添加订单成功', 'list' => $list);
}else{
	$arr = array('code' => 400, 'msg' => 'userid或shopid或buyFreightRisk或carid不存在,可传参数addressid');
}

echo json_encode($arr);

?>